package com.dts.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dts.Mapper.UserMapper;
import com.dts.advice.LyqException;
import com.dts.entity.User;
import com.dts.service.UserService;
import com.dts.utils.JwtUtil;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public String login(String username, String password) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getName,username);
        wrapper.eq(User::getPassword,password);
        User user = baseMapper.selectOne(wrapper);
        if(user == null){
           throw new LyqException("没有该用户");
        }
        return JwtUtil.generateToken(user.getName());
    }

    @Override
    public void register(String username, String password) {
        User user = new User(username, password);
        user.setCreateTime(new Date());
        baseMapper.insert(user);
    }
}
